package com.sto.stoapp.manager;

import android.content.Context;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.http.BaseMessage;

import java.util.ArrayList;
import java.util.List;


public class AccessControlManager extends BaseMessage {

	private String ProgramRole;

	// private String DevRole;

	private boolean JobSelect;

	// private boolean CarManager;

	private boolean ExpressQuery;

	private Context context;

	/**
	 * 扫描员
	 */
	public static final String SCANNER = "S";

	/**
	 * 业务员
	 */
	public static final String SALES = "Y";

	/**
	 * 管理员
	 */
	public static final String ADMINTER = "G";

	/**
	 * 提货员
	 */
	public static final String PICKUP = "T";

	/**
	 * 所有
	 */
	public static final String ALL = "A";

	/**
	 * 全业务(不含管理员)
	 */
	public static final String FULL_BUSI = "F";

	private static final String OBJECT_LOCK = "AccessControl";

	private static AccessControlManager accessControl;

	private AccessControlEntity[] roleType;

	private AccessControlManager() {
		// TODO Auto-generated constructor stub
	}

	private AccessControlManager(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	/**
	 * 得到单例化对象
	 * 
	 * @param context
	 * @return
	 */
	public static AccessControlManager getInstance(Context context) {
		synchronized (OBJECT_LOCK) {
			if (accessControl == null) {
				accessControl = new AccessControlManager(
						StoApplication.getAppInstance());
			}

			return accessControl;
		}
	}

	/**
	 * 读取配置信息
	 */
	public void initConfig() {
		/* 程序角色 */
		ProgramRole = StoFilePreference.getInstance(context)
				.getProgramRoleControl();
		/* 巴枪角色 */
		// DevRole = StoFilePreference.getInstance(context).getBarRoleControl();
		/* 是否允许操作员 */
		JobSelect = StoFilePreference.getInstance(context)
				.getSelectedOperatorControl();
		/* 车辆管理 */
		// CarManager =
		// StoFilePreference.getInstance(context).getVehicleManagerControl();
		/* 快件查询 */
		ExpressQuery = StoFilePreference.getInstance(context)
				.getExpressSearchControl();

		roleType = new AccessControlEntity[] {
				new AccessControlEntity(
						SALES_VERSION, // 网点"N"
						new RoleAttrEntity[] { new RoleAttrEntity("0-0", "A"),
								new RoleAttrEntity("0-1", "Z|W|X"), },
						new String[][] { { "10", ADMINTER }, { "20", SALES },
								{ "30", SCANNER } }),
				new AccessControlEntity(
						CENTER_VERSION, // 中心"C"
						new RoleAttrEntity[] { new RoleAttrEntity("1-0", "A"),
								new RoleAttrEntity("1-1", "X"), },
						new String[][] { { "10", ADMINTER }, { "30", SCANNER } }),
				new AccessControlEntity(
						AVIATION_A_VERSION, // 航空"R"
						new RoleAttrEntity[] { new RoleAttrEntity("0-1", "H"),
								new RoleAttrEntity("1-1", "Z|W|H"), },
						new String[][] { { "10", ADMINTER }, { "30", SCANNER },
								{ "40", PICKUP } }), };

		// roleType = new AccessControlEntity []{
		// new AccessControlEntity(
		// context.getText(R.string.site_point).toString(),
		// new RoleAttrEntity[]{
		// new RoleAttrEntity(SALES_VERSION + "W", SALES_VERSION),
		// new RoleAttrEntity(SCANNER_VERSION + "QX", SCANNER_VERSION ),//,
		// {SALES_VERSION + SCANNER_VERSION + "W", "SALES_VERSION"},
		// //{SCANNER_VERSION + "QX", "SCANNER_VERSION"},
		// }
		// ),
		//
		// new AccessControlEntity(
		// context.getText(R.string.center_point).toString(),
		// new RoleAttrEntity[]{
		// new RoleAttrEntity(CENTER_VERSION + AVIATION_Y_VERSION + "Z",
		// CENTER_VERSION ),
		// }
		// ) ,
		// new AccessControlEntity(
		// context.getText(R.string.aviation_point).toString(),
		// new RoleAttrEntity[]{
		// new RoleAttrEntity(AVIATION_Y_VERSION + AVIATION_A_VERSION + "WX",
		// AVIATION_A_VERSION),
		// new RoleAttrEntity(AVIATION_T_VERSION + "QZ", "T" ),
		// }
		// ),
		// };

	}

	/**
	 * 计算出登陆的响应码
	 * 
	 * @param responseCode
	 * @return
	 */
	public List<String> ProgramRole(String orgType, String orgAirType,
			List<String> roles) {

		List<String> meusRoles = null;
		String menus = "";
		if (roles != null && roles.size() > 0) {

			// 自动
			if (ProgramRole.equals(context.getText(R.string.automatic)
					.toString())) {

				meusRoles = findRoleAttr(orgType + "-" + orgAirType, "Z", roles);
			}
			// 网点
			else if (ProgramRole.equals(context.getText(R.string.site_point)
					.toString())) {
				// 网点
				meusRoles = findRoleAttr(orgType + "-" + orgAirType, "W", roles);

			}
			// 中心
			else if (ProgramRole.equals(context.getText(R.string.center_point)
					.toString())) {
				// 中心
				meusRoles = findRoleAttr(orgType + "-" + orgAirType, "X", roles);

			}
			// 航空
			else if (ProgramRole.equals(context
					.getText(R.string.aviation_point).toString())) {
				// 航空
				meusRoles = findRoleAttr(orgType + "-" + orgAirType, "H", roles);

			} else {

			}

			if (meusRoles != null) {
				for (String role : meusRoles) {

					menus += role;
				}
			}
		}
		System.out.println("menusrole:" + menus);

		StoFilePreference.getInstance(context).setRoleMenu(menus);
		return meusRoles;
	}

	/**
	 * 将字符串数据转成list string
	 * 
	 * @author wxf
	 * 
	 * @param menus
	 * @return
	 */
	public List<String> getListMenus(String menus) {
		List<String> menu = null;
		if (menus != null && !menus.equals("")) {

			menu = new ArrayList<String>();

			for (int i = 0; i < menus.length(); i++) {
				menu.add(String.valueOf(menus.charAt(i)));
			}

		}

		return menu;
	}

	/**
	 * 自动时判断
	 * 
	 * @param responseCode
	 * @return
	 */
	private String autoRoleAttr(String responseCode, String config,
			List<String> roles) {
		String role = "";
		String RoleAttr = "";

		// 程序角色
		// RoleAttr = findRoleAttr(responseCode, config, roles);

		return RoleAttr;
	}

	/**
	 * 
	 * @param responseCode
	 *            响应码
	 * @param programRole
	 * @return RoleAttr(type) or ""
	 */
	private List<String> findRoleAttr(String responseCode, String programRole,
			List<String> roles) {
		String roleAttr = "";
		// String menuRole="";
		List<String> menuRoles = null;

		for (AccessControlEntity entity : roleType) {
			for (RoleAttrEntity attrEntity : entity.getRoleAttr()) {

				if (attrEntity.getValue().equals(responseCode)) {

					if (attrEntity.getType().equals("A")
							|| attrEntity.getType().indexOf(programRole) > -1) {

						menuRoles = new ArrayList<String>();

						roleAttr = entity.getProgramRole();
						// menuRole = roleAttr;

						menuRoles.add(roleAttr);

						// 允许选择操作员
						if (JobSelect) {
							// 默认不包含管理员
							// menuRole += "F";

							if (roles.contains("10")) {
								// menuRole += "A"; // 含管理员
								menuRoles.add(ALL);

							} else {

								menuRoles.add(FULL_BUSI);
							}

						} else {
							// 禁止选择操作员
							String[][] tempMenus = entity.getRoleMenus();
							for (String role : roles) {
								for (String[] menus : tempMenus) {
									if (role.equals(menus[0])) {
										// menuRole += menus[1];
										menuRoles.add(menus[1]);
									}
								}
							}
						}

						return menuRoles;
					}

				}
			}
		}

		return menuRoles;
	}

	/**
	 * 快件查询状态
	 * 
	 * @return
	 */
	public boolean getExpressQuery() {

		return ExpressQuery;
	}

	public String getProgramRole() {
		String tempRole = StoFilePreference.getInstance(context)
				.getRoleMenu();
		if(!("").equals(tempRole)){
			tempRole = String.valueOf(tempRole.charAt(0));
		}else{
			tempRole ="";
		}
		

		return tempRole;
	}

	public String getRole() {
		String role = String.valueOf(StoFilePreference.getInstance(context)
				.getRoleMenu().charAt(0));

		if (SALES_VERSION.equals(role)) {
			return "0";
		}
		if (CENTER_VERSION.equals(role)) {
			return "1";
		}
		if (AVIATION_A_VERSION.equals(role)) {
			return "2";
		}

		return "";
	}

	/**
	 * 车辆管理状态
	 * 
	 * @return
	 */
	// public boolean getCarManager(){
	//
	// return CarManager;
	// }

	/**
	 * 是否允许操作员
	 * 
	 * @return
	 */
	public boolean getJobSelect() {

		return JobSelect;
	}

	class AccessControlEntity {
		private String ProgramRole;
		private RoleAttrEntity RoleAttr[];
		private boolean JobSelect;
		private String RoleMenus[][];

		public AccessControlEntity(String programRole,
				RoleAttrEntity roleAttr[], boolean jobSelect) {
			// TODO Auto-generated constructor stub
			this.ProgramRole = programRole;
			this.RoleAttr = roleAttr;
			this.JobSelect = jobSelect;
		}

		public AccessControlEntity(String programRole,
				RoleAttrEntity roleAttr[], String[][] roleMenus) {
			// TODO Auto-generated constructor stub
			this.ProgramRole = programRole;
			this.RoleAttr = roleAttr;
			this.RoleMenus = roleMenus;
		}

		public String getProgramRole() {
			return ProgramRole;
		}

		public RoleAttrEntity[] getRoleAttr() {
			return RoleAttr;
		}

		public String[][] getRoleMenus() {
			return RoleMenus;
		}
	}

	class RoleAttrEntity {
		private String value;
		private String type;

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public RoleAttrEntity(String v, String t) {
			this.value = v;
			this.type = t;
		}
	}

}
